/* ---------------------------------------------------------------------------------------------
 *
 *            Capital Alliance Software Confidential Proprietary
 *            (c) Copyright CAS 201{x}, All Rights Reserved
 *                          www.pekall.com
 *
 * ----------------------------------------------------------------------------------------------
 */

package com.pekall.myDictionary.screens;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.pekall.myDictionary.utils.SQLiteHistory;

public class HistoryScreen extends Activity implements OnItemClickListener {
    private ListView mListView;

    private WordAdapter mAdapter;

    private Context mContext;

    private List<String> mWords;

    private SQLiteHistory mSQL;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.history);

        mContext = this;
        mAdapter = new WordAdapter(mContext);
        mListView = (ListView) findViewById(R.id.historyList);
        mListView.setOnItemClickListener(this);
        mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        mListView.setAdapter(mAdapter);
    }

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(this, ExplainScreen.class);
        intent.setData(Uri.parse(mWords.get(position)));
        intent.putExtra("isCnOrEn", (mWords.get(position).getBytes().length == mWords.get(position)
                .length()));
        startActivity(intent);
        /** activity切换动画 **/
        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, Menu.FIRST + 1, Menu.NONE, R.string.empty_history).setIcon(
                android.R.drawable.ic_menu_delete);
        // menu.add(0, Menu.FIRST + 2, Menu.NONE, R.string.setting);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        switch (item.getItemId()) {

            case Menu.FIRST + 1:
                mSQL = new SQLiteHistory(mContext);
                mSQL.deleteTabel();
                mSQL.close();
                mAdapter = new WordAdapter(mContext);
                mListView.setAdapter(mAdapter);
                break;
        }
        return true;
    }

    class WordAdapter extends BaseAdapter {
        private SQLiteHistory mSQL;

        private LayoutInflater mFactory;

        public WordAdapter(Context context) {
            mFactory = LayoutInflater.from(context);
            mWords = new ArrayList<String>();
            mSQL = new SQLiteHistory(mContext);
            Cursor cur = mSQL.loadAll();
            StringBuffer sf = new StringBuffer();
            cur.moveToFirst();
            while (!cur.isAfterLast()) {
                sf.append(cur.getInt(0)).append(" : ").append(cur.getString(1)).append("\n");
                mWords.add(cur.getString(1));
                cur.moveToNext();
            }
            mSQL.close();
        }

        public int getCount() {
            return mWords.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return 0;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                v = mFactory.inflate(android.R.layout.simple_list_item_1, parent, false);
            }
            TextView tv = (TextView) v.findViewById(android.R.id.text1);
            int n = mWords.get(position).indexOf('\t');
            if (n > 0) {
                tv.setText(mWords.get(position).substring(0, n));
            } else
                tv.setText(mWords.get(position));
            return v;
        }

        @Override
        public void notifyDataSetChanged() {
            super.notifyDataSetChanged();

        }

    }

}
